
package com.android.interfata;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.android.menu.MenuAbout;
import com.android.menu.MenuContact;
import com.android.menu.MenuHelp;
import com.android.menu.MenuLegal;
import com.sonyericsson.zoom.BasicZoomControl;
import com.sonyericsson.zoom.ImageZoomView;
import com.sonyericsson.zoom.LongPressZoomListener;

/**
 * Activity for zoom tutorial 1
 */
public class Statistics extends Activity
{
	
    /** Constant used as menu item id for resetting zoom state */
    private static final int MENU_ID_RESET = 0;
    /** Image zoom view */
    private ImageZoomView mZoomView;
    /** Zoom control */
    private BasicZoomControl mZoomControl;
    /** Decoded bitmap image */
    private Bitmap mBitmap;
    /** On touch listener for zoom view */
    private LongPressZoomListener mZoomListener;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.statistics);

		try
		{
			String url0;
			String prodTitle;
			Intent i = getIntent();
			url0 = i.getStringExtra("url");
			
			prodTitle = i.getStringExtra("prodTitle");
			TextView produs = (TextView) findViewById(R.id.zoomtitle);
			produs.setText(prodTitle);
			
			
			System.out.println("url Statistics " + url0);
			
			if (url0.equals(""))
				url0 = "http://mail.pioneerinvestments.ro:333/images/img.jpg";
			
			// incarc poza
			Drawable image = ImageOperations(getApplicationContext(), url0, "image.jpg");
	
			mZoomControl = new BasicZoomControl();
	        //mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image800x600);
			mBitmap = ((BitmapDrawable)image).getBitmap();
	        Log.d("url", url0);
			
	        mZoomListener = new LongPressZoomListener(getApplicationContext());
	        mZoomListener.setZoomControl(mZoomControl);
	
	        mZoomView = (ImageZoomView)findViewById(R.id.zoomview);
	        mZoomView.setZoomState(mZoomControl.getZoomState());
	        mZoomView.setImage(mBitmap);
	        mZoomView.setOnTouchListener(mZoomListener);
	
	        mZoomControl.setAspectQuotient(mZoomView.getAspectQuotient());
	
	        resetZoomState();
			
		}
		catch(Exception e)
		{
			System.out.println("Statistics " + e);
		}
        
        
        
        
        
    }



	/*       incarc poza din url        */
	public Drawable ImageOperations(Context ctx, String url, String saveFilename)
	{
	    try 
	    {
	        InputStream is = (InputStream) this.fetch(url);
	        Drawable d = Drawable.createFromStream(is, "src");
	        return d;
	    } catch (MalformedURLException e) {
	        return null;
	    } catch (IOException e) {
	        return null;
	    }
	}
	
	public Object fetch(String address) throws MalformedURLException,IOException {
	    URL url = new URL(address);
	    Object content = url.getContent();
	    return content;
	}
	
	/*       end incarc poza din url        */

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mBitmap.recycle();
        mZoomView.setOnTouchListener(null);
        mZoomControl.getZoomState().deleteObservers();
    }

    
    
    
    
    
  
    /**
     * Reset zoom state and notify observers
     */
    private void resetZoomState() {
        mZoomControl.getZoomState().setPanX(0.5f);
        mZoomControl.getZoomState().setPanY(0.5f);
        mZoomControl.getZoomState().setZoom(1f);
        mZoomControl.getZoomState().notifyObservers();
    }
 
    

	/*     menu      */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu1, menu);
        
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	Intent intent;
        switch (item.getItemId()) 
        {
            case R.id.menu_about:     
            	intent = new Intent(getApplicationContext(), MenuAbout.class);
				startActivity(intent);
                break;
            case R.id.menu_help:     
            	intent = new Intent(getApplicationContext(), MenuHelp.class);
				startActivity(intent);
                break;
            case R.id.menu_legal: 
            	intent = new Intent(getApplicationContext(), MenuLegal.class);
				startActivity(intent);
            	break;
            case R.id.menu_contact:
            	intent = new Intent(getApplicationContext(), MenuContact.class);
				startActivity(intent);
            	break;
            case R.id.menu_reset:
            	resetZoomState();
                break;
        }
        return true;
    }
    
}
